package cn.kent.middle;

import java.util.Arrays;

/**
 * 881. 救生艇
 * 注意审题：每个船只能坐两个人
 */
public class NumRescueBoats {
    public static void main(String[] args) {
        int[] p = {1, 1, 1, 1, 1};
        int limit = 6;
        final int i = numRescueBoats(p, limit);
        System.out.println(i);
    }

    /**
     * 贪心算法  双指针
     */
    public static int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        final int len = people.length;
        int l = 0;
        int r = len - 1;
        int res = 0;
        // 双指针
        while (l <= r) {
            if (people[r] + people[l] <= limit) {
                l++;
            }
            --r;
            ++res;
        }
        return res;
    }
}
